package com.douwong.student.view; /**
 * 工程名: colorpicker
 * 文件名: ColorPickerView.java
 * 包名: com.style.colorpicker
 * 日期: 2014-3-30上午10:14:15
 * Copyright (c) 2014
 *
 */


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.douwong.student.R;


/**色盘
 * 类名: ColorPickerView <br/>
 * 功能: 颜色选择器. <br/>
 * 日期: 2014-3-30 上午10:14:15 <br/>
 * 
 * @author msl
 * @version
 */
public class ColorPickerView extends FrameLayout {

	private ImageView iv_color_range;// 颜色选择盘

	private ImageView iv_color_picker;// 颜色选择器

	private RelativeLayout rl_root;// 根布局

	private int range_radius, range_y;// 圆盘半径
	private int picker_radius;// 颜色选择器半径
	private int centreX;// 圆盘中心X坐标
	private int centreY;// 圆盘中心Y坐标
	private int picker_centreX;// 颜色选择器中心X坐标
	private int picker_centreY;// 颜色选择器中心Y坐标
	private Bitmap bitmap;// 颜色选择盘图片
	private int pix;
	private TextView center_text;
	public onColorChangedListener colorChangedListener;// 颜色变换监听

	public ColorPickerView(Context context) {
		super(context);

	}
	public ColorPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * 
	 * init:(初始化). <br/>
	 * 
	 * @author msl
	 * @param context
	 * @since 1.0
	 */
	@SuppressLint("NewApi")
	private void init(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.color_picker,
				this);
		iv_color_range = (ImageView) view.findViewById(R.id.iv_color_range);
		iv_color_picker = (ImageView) view.findViewById(R.id.iv_color_picker);
		center_text=(TextView) view.findViewById(R.id.center_text);
		rl_root = (RelativeLayout) view.findViewById(R.id.rl_root);
		// // // 选择器触摸监听
		iv_color_range.setOnTouchListener(new OnTouchListener() {
			int lastX, lastY;// 上次触摸坐标
			int dx, dy;
			int left, top, right, bottom;
			float diff;
			int dwonX, dwonY;
			float length;// 斜边长度

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.e("center_text_top",center_text.getTop()+"");
				Log.e("center_text_bottom",center_text.getBottom()+"");
				Log.e("center_text_left",center_text.getLeft()+"");
				Log.e("center_text_right",center_text.getRight()+"");
				int ea = event.getAction();
				if (range_radius == 0) {// 未初始化
					range_radius = iv_color_range.getWidth() / 2;// 圆盘半径
					picker_radius = iv_color_picker.getWidth() / 2;// 选择器半径
					range_y = iv_color_range.getHeight() / 2;// 图片高度/2
					centreX = iv_color_range.getRight() - range_radius;
					centreY = iv_color_range.getBottom() - range_y;
					Log.e("range_y", range_y + "");
					length = (float) Math.sqrt((float)(centreX * centreX + centreY
							* centreY));
					bitmap = ((BitmapDrawable) iv_color_range.getDrawable())
							.getBitmap();// 获取圆盘图片
				}
				switch (ea) {
				case MotionEvent.ACTION_DOWN:// 按下
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					dwonX = (int) event.getX();
					dwonY = (int) event.getY();
					Log.e("dwonX",""+dwonX);
					Log.e("dwonY",""+dwonY);
					left = dwonX - picker_radius;
					top = dwonY - picker_radius;
					right = dwonX + picker_radius;
					bottom = dwonY + picker_radius;
					Log.e("", "down:" + event.getX() + "   y:" + event.getY());
					// 选择器圆心与圆盘圆心距离
					picker_centreX = right - picker_radius;
					picker_centreY = bottom - picker_radius;

					diff =(float) Math.sqrt((float)((centreY - picker_centreY)
							* (centreY - picker_centreY)
							+ (centreX - picker_centreX)
							* (centreX - picker_centreX)))
							+ picker_radius / 2;// 两个圆心距离+颜色选择器半径
					float len = (float) Math.sqrt((float)((dwonX - centreX)
							* (dwonX - centreX) + (dwonY - centreY)
							* (dwonY - centreY)));
					// 在边距内，则拖动
					Log.e("downdiff", "" + diff);
					Log.e("down---------", left + "----" + top + "----" + right
							+ "----" + bottom);
					if (len <= range_radius && diff <= range_radius) {
						System.out.println("============>>>down移动");
						iv_color_picker.layout(left, top, right, bottom);
						
						int x=picker_centreX
								- iv_color_range.getLeft();
						int y=picker_centreY
								- iv_color_range.getTop();
						Log.e("x",""+x);
						Log.e("y",""+y);
						if(x<center_text.getLeft()||x>center_text.getRight()||y<center_text.getTop()||y>center_text.getBottom()){
							Log.e("取色按下","取色按下");
							if(x<bitmap.getWidth()&&y<bitmap.getHeight()){
								pix = bitmap.getPixel(x, y);// 获取选择器圆心像素
							}
							if (colorChangedListener != null) {// 读取颜色
								
								colorChangedListener.colorChanged(Color.red(pix),
										Color.blue(pix), Color.green(pix),
										pix);
							}
							
						}
							
						
					}
					// 通知父控件勿拦截本控件touch事件
					getParent().requestDisallowInterceptTouchEvent(true);//
					break;
				case MotionEvent.ACTION_MOVE:// 拖动
					try {
						Log.e("",
								"move:" + event.getX() + "   y:" + event.getY());
						// 拖动距离
						dx = (int) event.getRawX() - lastX;
						dy = (int) event.getRawY() - lastY;
						// 相对于父控件的新坐标
						// 选择器圆心坐标
						left = iv_color_picker.getLeft() + dx;
						top = iv_color_picker.getTop() + dy;
						right = iv_color_picker.getRight() + dx;
						bottom = iv_color_picker.getBottom() + dy;
						picker_centreX = right - picker_radius;
						picker_centreY = bottom - picker_radius;
						float len1 = (float) Math.sqrt((dwonX - centreX)
								* (dwonX - centreX) + (dwonY - centreY)
								* (dwonY - centreY));
						// 选择器圆心与圆盘圆心距离
						diff = (float) Math.sqrt((centreY - picker_centreY)
								* (centreY - picker_centreY)
								+ (centreX - picker_centreX)
								* (centreX - picker_centreX))
								+ picker_radius / 2;// 两个圆心距离+颜色选择器半径
						Log.e("movediff", "" + diff);

						// 在边距内，则拖动
						if (len1 <= range_radius && diff <= range_radius) {
							iv_color_picker.layout(left, top, right, bottom);
							
							int x=picker_centreX
									- iv_color_range.getLeft();
							int y=picker_centreY
									- iv_color_range.getTop();
							if(x<center_text.getLeft()||x>center_text.getRight()||y<center_text.getTop()||y>center_text.getBottom()){
								Log.e("取色按下","取色按下");
								if(x<bitmap.getWidth()&&y<bitmap.getHeight()){
									pix = bitmap.getPixel(x, y);// 获取选择器圆心像素
								}
								 
								if (colorChangedListener != null) {// 读取颜色
									
									colorChangedListener.colorChanged(Color.red(pix),
											Color.blue(pix), Color.green(pix),
											pix);
								}
								
							}
							lastX = (int) event.getRawX();
							lastY = (int) event.getRawY();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

					break;
				case MotionEvent.ACTION_UP:
					break;

				default:
					break;
				}
				return false;
			}
		});
	}

	public onColorChangedListener getColorChangedListener() {
		return colorChangedListener;
	}

	public void setColorChangedListener(
			onColorChangedListener colorChangedListener) {
		this.colorChangedListener = colorChangedListener;
	}

	/**
	 * 颜色变换监听接口
	 */
	public interface onColorChangedListener {
		/**
		 * 
		 * @param red
		 * @param blue
		 * @param green
		 * @param pixel
		 *            颜色值
		 */
		public void colorChanged(int red, int blue, int green, int pixel);
	}

}
